﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FisApp.enums;
using FisApp.utility;
using System.Data.SqlClient;

namespace FisApp
{
    public partial class FrmTaban : Form
    {
        #region Variables...
        private string tabanId;
        private string sqlCommandType = SqlCommandType.INSERT;

        public int? kalite
        {
            get
            {
                return (int?)kaliteLookUp.EditValue;
            }
            set
            {
                kaliteLookUp.EditValue = value;
            }
        }

        public string tabanMarka
        {
            get
            {
                return txtTabanMarka.Text;
            }
            set
            {
                txtTabanMarka.Text = value;
            }
        }

        public string tabanNo
        {
            get
            {
                return txtTabanNo.Text;
            }
            set
            {
                txtTabanNo.Text = value;
            }
        }

        public string tabanRenk
        {
            get
            {
                return txtTabanRenk.Text;
            }
            set
            {
                txtTabanRenk.Text = value;
            }
        }

        public DataTable Tabanlar
        {
            get
            {
                return (DataTable)gcTaban.DataSource;
            }

            set
            {
                gcTaban.DataSource = value;
            }
        }

        public DataRow SelectedRow
        {
            get
            {
                return gvTaban.GetDataRow(gvTaban.FocusedRowHandle);
            }
        }
        #endregion

        #region Constructor...
        public FrmTaban()
        {
            InitializeComponent();
        }
        #endregion

        #region Methods...
        private void clearForm()
        {
            tabanId = null;
            kalite = null;
            tabanMarka = null;
            tabanNo = null;
            tabanRenk = null;
            btnVazgec.Visible = false;
            sqlCommandType = SqlCommandType.INSERT;
        }

        private void LoadTabanlar()
        {
            try
            {
                string query = "SELECT * FROM taban t, kalite k WHERE t.kalite_id = k.kalite_id";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                    Tabanlar = ds.Tables[0];
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void LoadKalite()
        {
            try
            {
                string query = "SELECT k.*, kalite_no + ' - ' + kalip_no as kalite_kalip FROM kalite k";
                DataSet ds = DBUtils.SQLSelect(query, null);
                if (ds == null)
                    return;

                if (ds.Tables.Count != 0)
                {
                    kaliteLookUp.Properties.DataSource = ds.Tables[0];
                    kaliteLookUp.Properties.DisplayMember = "kalite_kalip";
                    kaliteLookUp.Properties.ValueMember = "kalite_id";
                }
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Hata :" + ex.Message, MessageTypes.Error);
            }
        }
        #endregion

        #region Events...
        private void FrmTaban_Load(object sender, EventArgs e)
        {
            LoadKalite();
            LoadTabanlar();
        }

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            if (kalite == null)
            {
                FrmMessage.showDialog("Lütfen kalite no giriniz.", MessageTypes.Warning);
                return;
            }
            if (string.IsNullOrEmpty(tabanMarka))
            {
                FrmMessage.showDialog("Lütfen taban markası no giriniz.", MessageTypes.Warning);
                return;
            }
            if (string.IsNullOrEmpty(tabanNo))
            {
                FrmMessage.showDialog("Lütfen taban no giriniz.", MessageTypes.Warning);
                return;
            }
            if (string.IsNullOrEmpty(tabanRenk))
            {
                FrmMessage.showDialog("Lütfen taban rengini giriniz.", MessageTypes.Warning);
                return;
            }

            try
            {
                if (sqlCommandType.Equals(SqlCommandType.INSERT))
                {
                    string command = "INSERT INTO taban (kalite_id, taban_marka, taban_no, taban_renk) VALUES (@kalite_id, @taban_marka, @taban_no, @taban_renk)";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@kalite_id", kalite));
                    list.Add(DBUtils.GetParameter("@taban_marka", tabanMarka));
                    list.Add(DBUtils.GetParameter("@taban_no", tabanNo));
                    list.Add(DBUtils.GetParameter("@taban_renk", tabanRenk));

                    DBUtils.ExecuteSQLCommand(command, list);
                }
                else if (sqlCommandType.Equals(SqlCommandType.UPDATE))
                {
                    string command = "UPDATE taban SET kalite_id=@kalite_id, taban_marka=@taban_marka, taban_no=@taban_no, taban_renk=@taban_renk WHERE taban_id=@taban_id";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@kalite_id", kalite));
                    list.Add(DBUtils.GetParameter("@taban_marka", tabanMarka));
                    list.Add(DBUtils.GetParameter("@taban_no", tabanNo));
                    list.Add(DBUtils.GetParameter("@taban_renk", tabanRenk));
                    list.Add(DBUtils.GetParameter("@taban_id", tabanId));

                    DBUtils.ExecuteSQLCommand(command, list);
                }

                LoadTabanlar();
                clearForm();
            }
            catch (Exception ex)
            {
                FrmMessage.showDialog("Kayıt işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void repbtnEdit_Click(object sender, EventArgs e)
        {
            tabanId = SelectedRow["taban_id"].ToString();
            kalite = Convert.ToInt32(SelectedRow["kalite_id"]);
            tabanMarka = SelectedRow["taban_marka"].ToString();
            tabanNo = SelectedRow["taban_no"].ToString();
            tabanRenk = SelectedRow["taban_renk"].ToString();
            sqlCommandType = SqlCommandType.UPDATE;
            btnVazgec.Visible = true;
        }

        private void repbtnDelete_Click(object sender, EventArgs e)
        {
            tabanId = SelectedRow["taban_id"].ToString();

            try
            {
                if(FrmMessage.ShowDialogResult(SelectedRow["taban_marka"].ToString() + " marka ve " + SelectedRow["taban_no"].ToString() + " no'lu taban bilgisini silmek istediğinize emin misiniz ?", MessageTypes.Question) == DialogResult.Yes)
                {
                    string command = "DELETE FROM taban WHERE taban_id=@taban_id";

                    List<SqlParameter> list = new List<SqlParameter>();
                    list.Add(DBUtils.GetParameter("@taban_id", tabanId));

                    DBUtils.ExecuteSQLCommand(command, list);

                    LoadTabanlar();
                    clearForm();
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.ToString().Contains("-2146232060"))
                    FrmMessage.showDialog(General.ForeignKeyExceptionMessage, MessageTypes.Error);
                else
                    FrmMessage.showDialog("Silme işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);

               // FrmMessage.showDialog("Silme işlemi yapılırken hata oluştu. Hata :" + ex.Message, MessageTypes.Error);
            }
        }

        private void btnVazgec_Click(object sender, EventArgs e)
        {
            clearForm();
        }

        private void FrmTaban_Activated(object sender, EventArgs e)
        {
            FrmTaban_Load(null, null);
        }
        #endregion
    }
}
